#!/bin/bash

SOURCE="$0"
while [ -h "$SOURCE"  ]; do 
    DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /*  ]] && SOURCE="$DIR/$SOURCE"
done
DIR="$( cd -P "$( dirname "$SOURCE"  )" && pwd  )"

function CompileDependents () {
    chmod -R 777 ./buildDependents.sh
    bash ./buildDependents.sh
    if [ $? -eq 0 ]; then
        echo "Compile Dependents success"
    else
        echo "Compile failed"
        exit -1
    fi
}

function CompileFFmpeg () {
    git clone https://gitee.com/mirrors/ffmpeg.git
    cd ffmpeg 
    mkdir buildd && cd buildd 

    local myconf=(
        --arch=arm64 --target-os=mingw32 --cross-prefix=aarch64-w64-mingw32- --pkg-config=pkg-config --host-cflags=-I/build/ffmpeg-deps/include --host-ldflags=-I/build/ffmpeg-deps/include --extra-cflags="-I${FFBUILD_PREFIX}/include -static-libgcc -w -pipe -O3 -DNDEBUG -DLIBTWOLAME_STATIC" --extra-cxxflags="-I${FFBUILD_PREFIX}/include -static-libgcc -static-libstdc++ -w -pipe -O3 -DNDEBUG" --pkg-config-flags=--static
        --enable-version3
        --disable-debug
        --disable-w32threads 
        --enable-pthreads
        --enable-iconv
        --enable-libxml2
        --enable-zlib
        --enable-libfreetype
        --enable-libfribidi
        --enable-gmp
        --enable-lzma
        --enable-fontconfig
        --enable-libvorbis
        --enable-opencl
        --enable-libvmaf
        --enable-libaom
        --enable-libdav1d
        --enable-libfdk-aac
        --enable-libgme
        --enable-libass
        --enable-libbluray
        --enable-libmp3lame
        --enable-libopus
        --enable-libtheora
        --enable-libvpx
        --enable-libwebp
        --enable-libopencore-amrnb --enable-libopencore-amrwb
        --enable-libopenh264
        --enable-libopenjpeg
        --enable-libopenmpt
        --enable-schannel
        --enable-sdl2
        --enable-libsoxr
        --enable-libsrt
        --enable-libsvtav1
        --enable-libzimg
        --enable-libtwolame
        --enable-librist
    )

    local static=(
        --enable-static --disable-shared --extra-ldflags="-L${FFBUILD_PREFIX}/lib -static-libgcc -static-libgcc -Wl,-Bstatic -lpthread"
    )
    local shared=(
        --enable-shared --disable-static --extra-ldflags="-L${FFBUILD_PREFIX}/lib -static-libgcc -static-libgcc -Wl,-Bstatic -lpthread -Wl,-Bdynamic"
    )
    local gpl=(
        --enable-gpl
        --enable-nonfree
        --enable-avisynth
        --enable-frei0r
        --enable-librubberband
        --enable-libvidstab
        --enable-libx264
        --enable-libx265
        --enable-libxvid
    )

    # static with lgpl
    ../configure "${myconf[@]}" "${static[@]}" --prefix=/build/ffmpeg-aarch64-static-lgpl 
    make -j$(nproc)
    make install install-doc

    rm -rf *

    # shared with lgpl
    ../configure "${myconf[@]}" "${shared[@]}" --prefix=/build/ffmpeg-aarch64-shared-lgpl 
    make -j$(nproc)
    make install install-doc

    rm -rf *

    # static with gpl
    ../configure "${myconf[@]}" "${static[@]}" "${gpl[@]}" --prefix=/build/ffmpeg-aarch64-static-gpl 
    make -j$(nproc)
    make install install-doc

    rm -rf *

    # shared with gpl
    ../configure "${myconf[@]}" "${shared[@]}" "${gpl[@]}" --prefix=/build/ffmpeg-aarch64-shared-gpl 
    make -j$(nproc)
    make install install-doc

    rm -rf *
}

CompileDependents
CompileFFmpeg

